package com.knightboy.smartshoes.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.knightboy.smartshoes.R;

import java.util.ArrayList;

/**
 * Created by 29288 on 2015/9/17.
 */
public class DataFragment extends Fragment {
    //所需字符串
    final String title1 = "最近七天步数图(单位:百步)";
    final String title2 = "最近七天卡路里图(单位:百卡路里)";
    final String title3 = "最近七天路程图(单位:公里)";

    //控件定义
    private BarChart mBarChart1, mBarChart2, mBarChart3;
    private BarData mBarData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_data,container,false);
        //设置三个图的数据
        mBarChart1 = (BarChart)rootview.findViewById(R.id.mBarChart1);
        mBarData = getBarData(7, 100, title1);
        showBarChart(mBarChart1, mBarData);

        mBarChart2 = (BarChart)rootview.findViewById(R.id.mBarChart2);
        mBarData = getBarData(7, 100, title2);
        showBarChart(mBarChart2, mBarData);

        mBarChart3 = (BarChart)rootview.findViewById(R.id.mBarChart3);
        mBarData = getBarData(7, 100, title3);
        showBarChart(mBarChart3, mBarData);
        return rootview;
    }

    private void showBarChart(BarChart barChart, BarData barData) {
        barChart.setDrawBorders(false);  ////是否在折线图上添加边框

        barChart.setDescription("");// 数据描述

        // 如果没有数据的时候，会显示这个，类似ListView的EmptyView
        barChart.setNoDataTextDescription("You need to provide data for the chart.");

        barChart.setDrawGridBackground(false); // 是否显示表格颜色
        barChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色，在这里是是给颜色设置一个透明度

        barChart.setTouchEnabled(true); // 设置是否可以触摸

        barChart.setDragEnabled(true);// 是否可以拖拽
        barChart.setScaleEnabled(true);// 是否可以缩放

        barChart.setPinchZoom(false);//

//      barChart.setBackgroundColor();// 设置背景

        barChart.setDrawBarShadow(true);

        barChart.setData(barData); // 设置数据

        Legend mLegend = barChart.getLegend(); // 设置比例图标示

        mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
        mLegend.setFormSize(6f);// 字体
        mLegend.setTextColor(Color.BLACK);// 颜色

        barChart.animateX(2500); // 立即执行的动画,x轴
    }

    private BarData getBarData(int count, float range, String title) {
        ArrayList<String> xValues = new ArrayList<String>();
        String[] week = new String[]{"周一","周二","周三","周四","周五","周六","周日"};
        for (int i = 0; i < count; i++) {
            xValues.add(week[i]);
        }

        ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();

        for (int i = 0; i < count; i++) {
            float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
            yValues.add(new BarEntry(value, i));
        }

        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, title);

        barDataSet.setColor(Color.rgb(27, 155, 244));

        ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
        barDataSets.add(barDataSet); // add the datasets

        BarData barData = new BarData(xValues, barDataSets);

        return barData;
    }
}
